package moonhy.c2dm_client.util;


import moonhy.c2dm_client.TabHostActivity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

public class networkConnectionManager extends BroadcastReceiver{
	String TAG = "C2DM";
	static final String ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
	private boolean mActiveNetState = false;
	private boolean mMobileNetState = false;
	private boolean mStatus = false;
	
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();

        boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
//        String reason = intent.getStringExtra(ConnectivityManager.EXTRA_REASON);
//        boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);
//        NetworkInfo currentNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
//        NetworkInfo otherNetworkInfo = (NetworkInfo) intent.getParcelableExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO);
//        Log.i(TAG, "Status : " + noConnectivity + ", Reason :" + reason + ", FailOver :" + isFailover + ", Current Network Info : " + currentNetworkInfo + ", OtherNetwork Info :" + otherNetworkInfo);

        mStatus = noConnectivity;
        if(!mStatus){
	        //Toast.makeText(context, "3G or Wifi is not available!", Toast.LENGTH_LONG).show();
        	
        	Intent i = new Intent(context, TabHostActivity.class);
			//i.putExtra("payload", payload);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
        }
    }
	
	public void networkStatus(Context context, Intent intent){
		boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
		mStatus = noConnectivity;
        if(mStatus){
	        Toast.makeText(context, "3G or Wifi is not available!", Toast.LENGTH_LONG).show();
        }
	}


	
	/*
	private void netCheckin() {

        try {

            ConnectivityManager nInfo = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            nInfo.getActiveNetworkInfo().isConnectedOrConnecting();

            Log.d(tag, "Net avail:"
                    + nInfo.getActiveNetworkInfo().isConnectedOrConnecting());

            ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            if (netInfo != null && netInfo.isConnectedOrConnecting()) {
               Toast.makeText(getApplicationContext(), "net on", Toast.LENGTH_LONG).show();
                Log.d(tag, "Network available:true");

            } else {
                 Toast.makeText(getApplicationContext(), "net OFF", Toast.LENGTH_LONG).show();
                Log.d(tag, "Network available:false");
            }

        } catch (Exception e) {
           Log.e("error",""+e);
        }
    }*/
}
